package com.caocao.controller;

import com.caocao.domain.AccountConst;
import com.caocao.domain.RequestResult;
import com.caocao.domain.model.TBilltradedeatil;
import com.caocao.domain.model.TUser;
import com.caocao.service.BillService;
import com.caocao.utils.RequestResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 描述:账单使用相关业务逻辑控制器
 *
 * @Author:操文健
 * @Create Time:2017-09-15 21:11
 */
@RestController
@RequestMapping(value = "/bill")
public class BillDetailController {

    private static final Logger logger = LoggerFactory.getLogger(BillDetailController.class);

    @Autowired
    private BillService billService;

    /**
     * 交易记录明细接口
     * @param request
     * @return
     */
    @PostMapping(value = "/record")
    public RequestResult recordDetail(HttpServletRequest request) {
        // 通过请求信息获取session信息中的登录用户信息
        TUser user = (TUser) request.getSession().getAttribute(AccountConst.SESSION_USER_LOGINGO);
        logger.info("进入账单明细记录方法,当前登录人员为:" + user.getUsername());
        Map<String, Object> param = new HashMap<String, Object>();
        // 开始根据请求信息获取请求中的属性值
        param.put("billType", request.getParameter("billType"));//记账的类型
        param.put("totalMoney", request.getParameter("totalMoney"));//总金额
        logger.info("界面记录日期为： " +  request.getParameter("recordDate"));
        param.put("recordDate", request.getParameter("recordDate"));//记录日期
        param.put("costUsers", request.getParameter("costUsers"));//消费均摊用户信息
        param.put("description", request.getParameter("description"));//账单描述,备注信息

        /**   后台开始业务逻辑数据处理 */
        if (billService.recordDetailAndAvgBil(user, param)) {
            return RequestResultUtil.success(user.getUsername());
        } else {
            return RequestResultUtil.error("账单记录出现异常");
        }
    }


    /**
     * 根据登录的相关信息获取对应的明细信息列表
     * @param userId
     * @param session
     * @return
     */
    @GetMapping(value = "/getCoderdDetail")
    @ResponseBody
    public RequestResult getRecordDetailForId (@RequestParam(value = "userId", defaultValue = "0", required = false) Integer userId,
                                               HttpSession session) {
        logger.info("当前查询的用户为:" + userId);
        TUser user = (TUser) session.getAttribute(AccountConst.SESSION_USER_LOGINGO);
        // 如果用户Id为空或者等于默认值0,则就根据session中的用户信息进行查询(当前登录用户)
        if (userId == null || userId == 0) {
            userId = user.getId();
        }
        Map<String, Object> resultMap = billService.queryBillDetailsByUser(userId);

        return RequestResultUtil.success(resultMap, user.getUsername());
    }

    /**
     * 查看每个人的消费金额信息
     * @return
     */
    @RequestMapping(value = "/spengRatio")
    @ResponseBody
    public RequestResult getspengRatioForEveryOne() {
        logger.info("查询每个人的消费消费金额信息...");
        List<Map<String, Object>> resultMap =  billService.spengRatioForEveryOne();
        return RequestResultUtil.success(resultMap, null);
    }

    @RequestMapping(value = "/billTypeRatio")
    @ResponseBody
    public RequestResult getBillTypeRatio() {
        logger.info("查询每种类型的消费比例信息...");
        List<Map<String, Object>> resultMap =  billService.billTypeRatio();
        return RequestResultUtil.success(resultMap, null);
    }
}
